package com.dream.service.recommender.toolkits.utils;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class StringUtils {

	/**
	 * 判断传入的char值是否阿拉伯数字，支持全角/半角形式
	 * @since 2007-12-03
	 * @param ch 传入的char值
	 * @return 是数字则返回true
	 */
	public static final boolean isArabianNumber(char ch) {
		return ch >= '0' && ch <= '9' || ch >= '０' && ch <= '９';
	}

	/**
	 * 判断传入的char值是否英文字母，支持大小写、全角/半角形式
	 * @since 2007-12-03
	 * @param ch 传入的char值
	 * @return 是字母则返回true
	 */
	public static final boolean isLetter(char ch) {
		return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= 'ａ'
				&& ch <= 'ｚ' || ch >= 'Ａ' && ch <= 'Ｚ';
	}

	/**
	 * @since 2007-12-03 原方法不支持全角形式的数字和字母
	 */
	public static boolean isDigitorAlpha(char ch) {
		return isArabianNumber(ch) || isLetter(ch);
	}

	/**
	 * @author vein
	 * @since 2007-12-03 调用新的方法处理，原接口仍然可用
	 */
	public static boolean isAlpha(char ch) {
		return isLetter(ch);
	}

	/**
	 *
	 * @since 2010-12-13 是否是指定的不需要过滤的特殊字符 如: "·"
	 */
	public static boolean isSpecialCharacter(char ch){
		if('·' == ch || ',' == ch || '，' == ch || '.' == ch){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 保留中文、字母和数字和空格
	 * @param ch
	 * @return
	 */
	public static String newSelectCJK(String ch) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < ch.length(); i++) {
			if ((Character.UnicodeBlock.of(ch.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) || (isDigitorAlpha(ch.charAt(i)))
					|| ch.charAt(i) == ' ') {
				sb.append(ch.charAt(i));
			}
		}
		return sb.toString();
	}

	/**
	 * 保留中文、字母和数字
	 */
	public static String selectCJK(String ch) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < ch.length(); i++) {
			if ((Character.UnicodeBlock.of(ch.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) || (isDigitorAlpha(ch.charAt(i)))) {
				sb.append(ch.charAt(i));
			}
		}
		return sb.toString();
	}

	/**
	 *  保留中文、字母和数字，其余替成空格
	 * @param ch
	 * @return
	 */
	public static String clearCJK(String ch) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < ch.length(); i++) {
			if ((Character.UnicodeBlock.of(ch.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isDigitorAlpha(ch.charAt(i)))) {
				sb.append(ch.charAt(i));
			} else {
				sb.append(" ");
			}
		}
		return sb.toString();
	}
	
	/**
	 * 包含了全角数字和字母，绝大多数标点符号（除“”‘’"'。.）
	 */
	private static boolean isOtherMarks(char ch) {
		return ((ch >= 0xfb00) && (ch <= 0xfffd))
				|| (Character.UnicodeBlock.of(ch) == Character.UnicodeBlock.GENERAL_PUNCTUATION);
	}

	/**
	 * 原方法与isMark()部分功能重复，目前范围主要包含。（中文句号）
	 * 
	 * @since 2007-12-03
	 */
	private static boolean isChineseMark(char ch) {
		return Character.UnicodeBlock.of(ch) == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION;
	}

	/**
	 * 判断传入的char值是否标点符号（不包含数字、字母），支持全角/半角形式
	 * 
	 * @since 2007-12-03
	 * @param ch 传入的char值
	 * @return 是标点则返回true
	 */
	public static boolean isMark(char ch) {
		if (isChineseMark(ch) || (!isDigitorAlpha(ch) && isOtherMarks(ch))) {
			return true;
		}
		if ((ch == '“') || (ch == '”') || ch == '‘' || ch == '’' || ch == '·') {
			return true;
		}

		if (ch <= 128 && (!isDigitorAlpha(ch))) {
			return true;
		}
		return false;
	}

	public static String StrRidMark(String chi) {
		StringBuffer sb = new StringBuffer();

		if (chi != null) {
			for (int i = 0; i < chi.length(); i++) {
				if (!isMark(chi.charAt(i))) {
					sb.append(chi.charAt(i));
				}
			}
		}
		return sb.toString();
	}

	public static boolean isDAString(String chi) {
		boolean ismarket = true;
		for (int i = 0; i < chi.length(); i++) {
			if (!StringUtils.isDigitorAlpha(chi.charAt(i))) {
				ismarket = false;
				break;
			}
		}
		return ismarket;
	}

	public static String getACJKString(String chi) {
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < chi.length(); i++) {
			if ((Character.UnicodeBlock.of(chi.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isAlpha(chi.charAt(i)))) {
				sb.append(chi.charAt(i));
			}
		}
		return sb.toString();
	}

	public static int getACJKStringLength(String chi) {
		int count = 0;

		for (int i = 0; i < chi.length(); i++) {
			if ((Character.UnicodeBlock.of(chi.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isAlpha(chi.charAt(i)))) {
				count++;
			}
		}
		return count;
	}

	public static boolean isMarkString(String chi) {
		boolean ismarket = true;
		for (int i = 0; i < chi.length(); i++) {
			if (!StringUtils.isMark(chi.charAt(i))) {
				ismarket = false;
				break;
			}
		}
		return ismarket;
	}

	public static String uencode(String ch) throws Exception {
		String tmp = "";
		for (int i = 0; i < ch.length(); i++) {
			String tt = ch.substring(i, i + 1);
			tt = URLEncoder.encode(tt, "utf-16");
			tt = tt.replaceAll("%FE%FF", "");
			tt = "\\u" + tt.replaceAll("%", "");
			tt = tt.toLowerCase();
			tmp += tt;
		}
		tmp += ",";
		return tmp;
	}

	public static boolean ismatch(String source,String pattern){
		return !isEmpty(source) && source.matches(pattern);
	}

	/**
	 * 是否是long
	 * @param content
	 * @return
	 */
	public static boolean isLong(String content) {
		if (isNumber(content)) {
			try {
				long num = Long.parseLong(content);
				return num <= Long.MAX_VALUE && num >= Long.MIN_VALUE;
			} catch (NumberFormatException e) {
				return false;
			}
		}
		return false;
	}

	/**
	 * 是否是int
	 * @param content
	 * @return
	 */
	public static boolean isInteger(String content) {
		if (isNumber(content)) {
			try {
				long num = Integer.parseInt(content);
				return num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE;
			} catch (NumberFormatException e) {
				return false;
			}
		}
		return false;
	}

	/**
	 * 是否是数字类型
	 * @param content
	 * @return
	 */
	public static boolean isNumber(String content) {
		return !isEmpty(content) && ismatch(content, "\\d+");
	}

	private static final Set<Character> prefixs = new HashSet<Character>();
	
	private static final Set<Character> postfixs = new HashSet<Character>();
	
	static {
		prefixs.add('(');
		prefixs.add('（');
		prefixs.add('[');
		prefixs.add('【');
		prefixs.add('〈');
		
		postfixs.add(')');
		postfixs.add('）');
		postfixs.add(']');
		postfixs.add('】');
		postfixs.add('〉');
	}
	
	
	/**
	 * 移除括号中的内容
	 * @return
	 */
	public static String removeBracketContent(String ch) throws Exception{
		StringBuilder sb = new StringBuilder();
		int separator_count = 0;
		for (int i = 0; i < ch.length(); i++) {
			if (prefixs.contains(ch.charAt(i))) {
				separator_count ++;
			} else if (postfixs.contains(ch.charAt(i))) {
				separator_count --;
			} else {
				if (separator_count == 0) {
					sb.append(ch.charAt(i));
				}
			}
		}
		return sb.toString();
	}

	public static boolean isEmpty(String sequence) {
		if (sequence == null || sequence.equals("")) {
			return true;
		}
		return false;
	}

	public static boolean isEmpty(Collection<? extends Object> models) {
		return models == null || models.isEmpty();
	}
}
